package user8086.controller;


import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import user8086.entity.param.UserAddParam;
import user8086.entity.param.UserLoginParam;
import user8086.entity.po.User;
import user8086.entity.vo.ResultVO;
import user8086.service.UserService;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ttxxi
 * @since 2022-05-31
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    UserService userService;


    @ApiOperation("登录")
    @PostMapping("/login")
    public ResultVO<Long> login(@RequestBody UserLoginParam param){
        Long userId = userService.login(param);
        if (userId!=null){
            return new ResultVO.SUCCESS<>(userId);
        }else {
            return new ResultVO.FAIL<>();
        }
    }

    @ApiOperation("查询所有用户")
    @GetMapping
    public ResultVO<List<User>> getAll(){
        List<User> list = userService.list();
        return new ResultVO.SUCCESS<>(list);
    }

    @ApiOperation("新增用户")
    @PostMapping
    public ResultVO addUser(@RequestBody UserAddParam param){
        Boolean res = userService.addUser(param);
        if (res){
            return new ResultVO.SUCCESS<>();
        }else {
            return new ResultVO.FAIL<>();
        }
    }

    @ApiOperation("删除")
    @DeleteMapping
    public ResultVO deleteUser(Long id){
        boolean res = userService.removeById(id);
        if (res){
            return new ResultVO.SUCCESS<>();
        }else {
            return new ResultVO.FAIL<>();
        }
    }

}

